package enterprise.manage.web.controller.product;

import enterprise.manage.dao.ProductDao;
import enterprise.manage.dao.ProductTypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User:zhenmingyue
 * Date: 2012-12-23
 * Desc:
 */
public class ProductAjaxController extends AbstractController {
    @Autowired
    private ProductTypeDao productTypeDao;

    protected ModelAndView handleRequestInternal(HttpServletRequest request
            , HttpServletResponse response) {
        String method = request.getParameter("method");
        String o = "";
        if ("getBrandOption".equals(method)) {
            String type = request.getParameter("type");
            o = getProductBrandOptions(type);
        } else if ("getModelOption".equals(method)) {
            String type = request.getParameter("type");
            String brand = request.getParameter("brand");
            o =  getProductModelOptions(type ,brand);
        }
        writeBack(response,o);
        return null;
    }

    public String getProductBrandOptions(String type){
        return productTypeDao.getProductBrandSelectOption(type);
    }

    public String getProductModelOptions(String type, String brand) {
       return productTypeDao.getProductModelSelectOption(type,brand);
    }

    /**
     * 返回页面流
     *
     * @param httpServletResponse HttpServletResponse
     * @param returnString        String
     */
    public void writeBack(HttpServletResponse httpServletResponse, String returnString) {
        try {

            httpServletResponse.setHeader("Cache-Control", "no-cache");
            httpServletResponse.setContentType("text/html;charset=UTF-8");
            httpServletResponse.getWriter().write(returnString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
